#!/bin/bash

# sh docker.sh v1.0.0.25_1_7.1 test

# This shell is executed before docker build.
# 打包
GOOS=linux
GOARCH=amd64
CGO_ENABLED=0

# 读取输入参数
VERSION=$1
ENV=$2
TAG="v2-server"


# 配置公共变量
IMAGE_REGISTRY="registry.cn-shenzhen.aliyuncs.com/gfn/xiuxian"
IMAGE_NAME="${IMAGE_REGISTRY}:${TAG}.${VERSION}-${ENV}"
DOCKERFILE_PATH="manifest/docker/Dockerfile"
TargetPlatform="linux/amd64"
BUILD_CONTEXT="."  # 根据需要调整构建上下文

# 显示配置信息
echo "Docker 镜像名称: $IMAGE_NAME"
echo "Dockerfile 路径: $DOCKERFILE_PATH"
echo "版本号: $VERSION"
echo "标签: $TAG"
echo "构建上下文: $BUILD_CONTEXT"

echo "Go资产目录: $GO_DIST_PATH"

# 删除旧的构建产物
rm -rf $GO_DIST_PATH

echo "y" | gf build

# 检查 Go 构建是否成功
if [ $? -eq 0 ]; then
    echo "Go build 成功，开始 Docker 镜像构建..."
    # 构建 Docker 镜像
    docker build --platform $TargetPlatform -f $DOCKERFILE_PATH -t $IMAGE_NAME $BUILD_CONTEXT
    docker login --username=深圳下楼科技 --password=vaY6lFd9HSV2guDD registry.cn-shenzhen.aliyuncs.com
    docker push $IMAGE_NAME
    echo "Docker 镜像构建完成，镜像已推送到阿里云镜像仓库"
    # 删除本地的镜像缓存
    docker rmi $IMAGE_NAME
    # 删除构建产物
    rm -rf $GO_DIST_PATH
else
    echo "Go build 失败，停止执行"
    exit 1
fi